package models

import (
	"time"

	"gorm.io/gorm"
)

// UserCardFeeConfig 用户卡费配置关联表（简洁版本）
type UserCardFeeConfig struct {
	ID              int64     `json:"id" gorm:"primaryKey;autoIncrement"`
	UserID          int64     `json:"user_id" gorm:"not null;index:idx_user_id"`
	CardFeeConfigID int64     `json:"card_fee_config_id" gorm:"not null;index:idx_card_fee_config_id"`
	Status          string    `json:"status" gorm:"not null;default:'active';index:idx_status"`
	CreatedAt       time.Time `json:"created_at" gorm:"not null;default:CURRENT_TIMESTAMP"`
	UpdatedAt       time.Time `json:"updated_at" gorm:"not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"`

	// 关联关系
	User          *User          `json:"user,omitempty" gorm:"foreignKey:UserID"`
	CardFeeConfig *CardFeeConfig `json:"card_fee_config,omitempty" gorm:"foreignKey:CardFeeConfigID"`
}

// TableName 指定表名
func (UserCardFeeConfig) TableName() string {
	return "user_card_fee_configs"
}

// BeforeUpdate GORM钩子：更新前
func (u *UserCardFeeConfig) BeforeUpdate(tx *gorm.DB) error {
	u.UpdatedAt = time.Now()
	return nil
}

// IsActive 检查配置是否激活
func (u *UserCardFeeConfig) IsActive() bool {
	return u.Status == "active"
}

// UserCardFeeConfigQuery 查询参数
type UserCardFeeConfigQuery struct {
	UserID          *int64  `json:"user_id"`
	CardFeeConfigID *int64  `json:"card_fee_config_id"`
	Status          *string `json:"status"`
	Page            int     `json:"page"`
	PageSize        int     `json:"page_size"`
}

// UserCardFeeConfigCreateRequest 创建请求
type UserCardFeeConfigCreateRequest struct {
	UserID          int64 `json:"user_id" binding:"required"`
	CardFeeConfigID int64 `json:"card_fee_config_id" binding:"required"`
}

// UserCardFeeConfigUpdateRequest 更新请求
type UserCardFeeConfigUpdateRequest struct {
	Status *string `json:"status"`
}
